namespace Budgetsch.WP7.ViewModel.Saving
{
    using System.Collections.ObjectModel;
    using System.Net;
    using System.Windows;
    using System.Windows.Input;

    using Budgetsch.WP7.Model.DataService;
    using Budgetsch.WP7.Model.Live;
    using Budgetsch.WP7.Navigation;

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Ioc;

    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class SavingViewModel : ViewModelBase
    {
        private readonly INavigationService _navigationService;

        private readonly ISavingDataService _SavingDataService;

        /// <summary>
        /// The <see cref="SavingListDetailsViewModels" /> property's name.
        /// </summary>
        public const string SavingListDetailsViewModelsPropertyName = "SavingListDetailsViewModels";

        private ObservableCollection<SavingListDetailsViewModel> _SavingListDetailsViewModels = new ObservableCollection<SavingListDetailsViewModel>();

        /// <summary>
        /// Sets and gets the SavingListDetailsViewModels property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<SavingListDetailsViewModel> SavingListDetailsViewModels
        {
            get
            {
                return _SavingListDetailsViewModels;
            }

            set
            {
                if (_SavingListDetailsViewModels == value)
                {
                    return;
                }

                _SavingListDetailsViewModels = value;
                RaisePropertyChanged(SavingListDetailsViewModelsPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="IsBusy" /> property's name.
        /// </summary>
        public const string IsBusyPropertyName = "IsBusy";

        private bool _isBusy;

        /// <summary>
        /// Sets and gets the IsBusy property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }

            set
            {
                if (_isBusy == value)
                {
                    return;
                }

                _isBusy = value;
                RaisePropertyChanged(IsBusyPropertyName);
            }
        }

        public ICommand SelectionCommand { get; set; }

        /// <summary>
        /// Initializes a new instance of the SavingViewModel class.
        /// </summary>
        public SavingViewModel(INavigationService navigationService, ISavingDataService SavingDataService)
        {
            _navigationService = navigationService;
            _SavingDataService = SavingDataService;
            if (IsInDesignModeStatic)
            {
                IsBusy = true;
                this.GetSavings();
            }
            SelectionCommand = new RelayCommand<SavingListDetailsViewModel>(p => { if (p != null) p.NavigateToDetailsView(); });
            
        }

        public void GetSavings()
        {
            IsBusy = true;
            _SavingDataService.GetSavings((list, exception) =>
            {
                if (exception != null)
                {
                    MessageBox.Show(exception.Message, "Error", MessageBoxButton.OK);
                }
                else
                {
                    SavingListDetailsViewModels.Clear();
                    foreach (var Saving in list)
                    {
                        SavingListDetailsViewModels.Add(new SavingListDetailsViewModel(Saving, _navigationService));
                    }
                }
                IsBusy = false;
            });
        }
    }
}